package com.homegoods.videomodule.ui.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.cyt.chattlibrary.face.CircleFlowIndicator;
import com.cyt.chattlibrary.face.FaceViewPager;
import com.cyt.chattlibrary.inputboard.InputPanelListener;
import com.cyt.chattlibrary.smoothsoftinputlayout.BaseSoftInputLayout;
import com.homegoods.videomodule.R;

/**
 * @author QiQi
 * @ClassName: LiveShowCommentSoftInputLayout
 * @date 2018/10/27
 * @description
 */
public class LiveShowCommentSoftInputLayout extends BaseSoftInputLayout {

    private View container;

    private ImageView btnEmotion;

    private View frame;
    private EditText editText;
    private View btnSend;
    private InputPanelListener listener;

    public LiveShowCommentSoftInputLayout(Context context) {
        super(context);
    }

    public LiveShowCommentSoftInputLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LiveShowCommentSoftInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void inflateView() {
        View layout = LayoutInflater.from(getContext()).inflate(R.layout.video_liveshow_comment_input_layout, this, true);
        container = layout.findViewById(R.id.face_bar);
        frame = layout.findViewById(android.R.id.widget_frame);
        editText = layout.findViewById(R.id.ed_comment);
        btnSend = layout.findViewById(R.id.tv_publish);

        btnSend.setOnClickListener(view -> {
            if (listener != null) {
                listener.onSendClick(editText.getText().toString());
            }
            editText.getText().clear();
        });

        setupEmotionView(layout);
    }

    private void setupEmotionView(View layout) {
        btnEmotion = layout.findViewById(R.id.face);
        btnEmotion.setOnClickListener(this);
        FaceViewPager emotionView = layout.findViewById(R.id.face_contain);

        CircleFlowIndicator indicator = layout.findViewById(R.id.layout_indicator_point);

        emotionView.setFlowIndicator(indicator);
        indicator.setCount(emotionView.getAdapter().getCount());
        emotionView.setEditText(editText);//设置表情输入的界面

        add2ShowViewList(emotionView);
        add2MappingMap(btnEmotion, SHOW_EMOTION, emotionView);// btnEmotion-(SHOW_EMOTION-emotionView)

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                btnSend.setEnabled(s.length() > 0);
            }
        });

    }

    @Override
    protected View getContainer() {
        return container;
    }

    @Override
    protected View getFrame() {
        return frame;
    }

    @Override
    public EditText getEditText() {
        return editText;
    }

    @Override
    protected View getBtnKeyBoard() {
        return null;
    }

    @Override
    protected void changeShowWhat(int showWhat) {
        super.changeShowWhat(showWhat);
        if (getShowWhat() == SHOW_EMOTION) {
            btnEmotion.setImageResource(R.drawable.btn_voice_to_text_selector);
        } else if (getShowWhat() == SHOW_KEYBOARD) {
            btnEmotion.setImageResource(R.drawable.btn_emoji_selector);
        }
    }

    public void setOnSendClickListener(InputPanelListener onSendClickListener) {
        listener = onSendClickListener;
    }
}
